package com.nageoffer.jobtrain.admin.service.filter;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nageoffer.jobtrain.admin.dao.entity.OjQuestionDO;
import com.nageoffer.jobtrain.admin.dao.mapper.OjQuestionMapper;
import com.nageoffer.jobtrain.admin.dto.req.OjQuestionAddReqDTO;
import com.nageoffer.jobtrain.admin.service.chain.OjQuestionAbstractChainHandler;
import com.nageoffer.jobtrain.common.web.exception.ClientException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import static com.nageoffer.jobtrain.admin.common.enums.ChainBizMarkEnum.OJ_QUESTION_CREATE_MARK;

/**
 * author：唐晨
 * Oj题目设置相识度匹配责任链实现
 */
@Component
@RequiredArgsConstructor
public class OjQuestionCreateParamVerifyChainFilter implements OjQuestionAbstractChainHandler<OjQuestionAddReqDTO> {
    private final OjQuestionMapper ojQuestionMapper;

    @Override
    public void handler(OjQuestionAddReqDTO requestParam) {
        // 这里目前相识度匹配，只按照题目标题进行匹配，后续的话可能会添加算法类匹配 ......
        LambdaQueryWrapper<OjQuestionDO> queryWrapper = Wrappers.lambdaQuery(OjQuestionDO.class)
                .eq(OjQuestionDO::getTitle, requestParam.getTitle())
                .eq(OjQuestionDO::getDelFlag, 0);
        OjQuestionDO ojQuestionDO = ojQuestionMapper.selectOne(queryWrapper);
        if (ojQuestionDO != null) {
            throw new ClientException("该Oj题目已存在题库中");
        }
    }

    @Override
    public String mark() {
        return OJ_QUESTION_CREATE_MARK.name();
    }

    @Override
    public int getOrder() {
        return 20;
    }
}
